package aspects;
import java.io.IOException;

import annotations.Skip;

public aspect AspectSkip {
	
	pointcut skipedMethods(Skip anno):
	     execution(@Skip * *(..)) && 
	     @annotation(anno);
		
	
	Object around(Skip anno) :  skipedMethods(anno) {
		Object res = null;
		try {
			res = proceed(anno);
		}catch(Exception e) {
			if(!ExceptionHandlingHelper.shouldHandleException(anno, e))
				throw new RuntimeException(e);
			
			if(e instanceof IOException)
				if(!handleIOException(anno, e))
					return res;
			
			Class myHandlingClass = anno.selfHandling();
			if(!myHandlingClass.equals(Object.class)){			
				if(!ExceptionHandlingHelper.invokeSelfHandling(myHandlingClass))
					return res;
			}
			
			System.out.println("Wyjatek: " + e);
		}
		return res;
	}
	
	private Boolean handleIOException(Skip anno, Exception e) {
		// specific actions for IOException
		Class myHandlingClass = anno.selfHandlingIOException();
		if(!myHandlingClass.equals(Object.class)){
			return ExceptionHandlingHelper.invokeSelfHandling(myHandlingClass);
		}
		return true;
	}
}
